package com.zygh.lz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zygh.lz.constant.SystemCon;
import com.zygh.lz.dao.SectionMapper;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.dao.XareaMapper;
import com.zygh.lz.dao.XdeclareMapper;
import com.zygh.lz.entity.Section;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.Xarea;
import com.zygh.lz.entity.Xdeclare;
import com.zygh.lz.service.XdeclareService;
import com.zygh.lz.stapprove.ResultUtil;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class XdeclareServiceImpl implements XdeclareService {
    @Autowired
    private XdeclareMapper xdeclareMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private XareaMapper xareaMapper;
    @Autowired
    private SectionMapper sectionMapper;

    /**
     * 新增申报
     *
     */
    @Override
    public ResultBeans insertXdeclare(Xdeclare xdeclare) {
        String now = DateUtil.now();
        xdeclare.setEstablishtime(now);
        int i = xdeclareMapper.insertSelective(xdeclare);
        return ResultUtil.resultBeansSuccess(i);
    }

    @Override
    public ResultBean selectDefaultMan(Integer staffid) {
        //查询当前登录人信息
        Staff staff = staffMapper.selectByPrimaryKey(staffid);
        //查询当前登录人直系领导
        Staff staff1 = staffMapper.selectStaffBypid(staff.getStafffPid());
        if (staff1 != null) {
            return ResultUtil.setOK("success", staff1);
        }
        return ResultUtil.setError(SystemCon.RERROR1, "error", "没有直系领导，请联系管理员！");
    }

    /**
     * 报备审批，审批通过后更新到岗位表
     *
     */
    @Override
    public ResultBean updateXdeclareByid(Xdeclare xdeclare) {
        int i = xdeclareMapper.updateByPrimaryKeySelective(xdeclare);
        Xdeclare xdeclare1 = xdeclareMapper.selectByPrimaryKey(xdeclare.getId());

        //大队
        Section section = sectionMapper.selectByPrimaryKey(Integer.valueOf(xdeclare1.getXareasection()));
        String ddui = section.getSectionName().substring(0, section.getSectionName().indexOf("队") + 1);
        //中队
        Section section1 = sectionMapper.selectByPrimaryKey(Integer.valueOf(xdeclare1.getDetachment()));
        String zdui = section1.getSectionName().substring(section1.getSectionName().indexOf("队") + 1, section1.getSectionName().length());
        System.out.println(zdui);
        Xarea xarea = new Xarea();
        if ("1".equals(xdeclare1.getState())) { //判断审批状态 通过
            if ("1".equals(xdeclare.getType())) {  //修改
                xarea.setId(xdeclare1.getXareaid());
                xarea.setName(xdeclare1.getXareaname());
                xarea.setStation(xdeclare1.getStation());
                xarea.setType(xdeclare1.getXareatype());
                xarea.setConment(xdeclare1.getConmnet());
                xarea.setCentre(xdeclare1.getContre());
                xarea.setStartTime(xdeclare1.getStarttime());
                xarea.setEndTime(xdeclare1.getStoptime());
                xarea.setBattalion(ddui);
                xarea.setDetachment(zdui);
                xarea.setSectionZid(Integer.valueOf(xdeclare1.getXareasection()));
                xarea.setSectionDid(Integer.valueOf(xdeclare1.getDetachment()));
                xareaMapper.updateByPrimaryKeySelective(xarea);


            } else if ("2".equals(xdeclare.getType())) {  //新增
                xarea.setName(xdeclare1.getXareaname());
                xarea.setStation(xdeclare1.getStation());
                xarea.setType(xdeclare1.getXareatype());
                xarea.setConment(xdeclare1.getConmnet());
                xarea.setCentre(xdeclare1.getContre());
                xarea.setStartTime(xdeclare1.getStarttime());
                xarea.setEndTime(xdeclare1.getStoptime());
                xarea.setBattalion(ddui);
                xarea.setDetachment(zdui);
                xarea.setSectionZid(Integer.valueOf(xdeclare1.getXareasection()));
                xarea.setSectionDid(Integer.valueOf(xdeclare1.getDetachment()));
                xareaMapper.insertSelective(xarea);

            }

        }

        return ResultUtil.execOp(i, "修改");
    }

    @Override
    public ResultBean deletedeclareByid(Integer id) {
        return ResultUtil.execOp(xdeclareMapper.deleteByPrimaryKey(id), "删除");
    }

    @Override
    public ResultBean findXdeclareByid(Integer appear, Integer accepter, String state) {
        return ResultUtil.setOK("success", xdeclareMapper.findXdeclareByid(appear, accepter, state));
    }


}
